package de.dopichaj.labrador.search.prune;

import java.util.SortedSet;

import de.dopichaj.labrador.search.hit.Hit;
import de.dopichaj.labrador.search.hit.HitHierarchyTree;
import de.dopichaj.labrador.search.merge.Merge;


public class ContextPruner implements ResultTreePruner {

    private final Merge merge;

    public ContextPruner(Merge merge) {
        this.merge = merge;
    }
    
    public void prune(HitHierarchyTree tree) {
        merge.setTree(tree);
        
        pruneRecursively(tree, tree.getRoot());
        
        merge.finishMerge();
    }
    
    /**
     * Prune the tree bottom-up.
     */
    private void pruneRecursively(HitHierarchyTree tree, Hit hit) {
        
        // process the children
        final SortedSet<Hit> children = tree.getChildren(hit);
        for (final Hit child : children) {
            pruneRecursively(tree, child);
        }
        
        // process this node
        if (children.size() > 0) {
            merge(tree, hit, children);
        }
    }

    /**
     * Applies merging to a single result, its siblings and parent.
     * @param children 
     */
    private void merge(HitHierarchyTree documentTree, Hit parentHit, SortedSet<Hit> children) {
        
        final int mergeDecision = merge.decideMerge(parentHit, children);
        
        if (mergeDecision == Merge.MERGE_UP) {
            mergeUp(parentHit, documentTree);                
        } else if (mergeDecision == Merge.MERGE_DOWN) {
            mergeDown(parentHit, documentTree);
        } else {
            doNotMerge(parentHit, documentTree, children);            
        }
    }
    
    /**
     * Merges the given node up.
     * Results are added to the HitHierarchyTree. 
     */
    public void mergeUp(Hit hit, HitHierarchyTree tree) {
        
        for (Hit child : tree.getChildren(hit)) {
            tree.removeFromResults(child);
        }
    }

    /**
     * Merges the current node down.
     * Results are added to the HitHierarchyTree. 
     */
    public void mergeDown(Hit hit, HitHierarchyTree tree) {
        tree.removeFromResults(hit);
    }

    /**
     * Does not merge the current node.
     * Results are added to the HitHierarchyTree. 
     * @param children 
     */
    public void doNotMerge(Hit hit, HitHierarchyTree tree, SortedSet<Hit> children) {
    }

    public String getDescription() {
        return merge.getDescription();
    }
}
/*
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/